Soru & Cevap

Neden calismadigi hk fikri olan var mi?

04.06.2014 - 01:18

[code]

AerItems.h

@interface AerItems : NSObject

@property (nonatomic,strong)NSString * weatherDay;

@property (nonatomic,strong)NSString *weatherText;

@property (nonatomic,strong)NSDate * weatherDate;

@property (nonatomic,strong)NSString *highTemp;

@property (nonatomic,strong)NSString *lowTemp;

-(instancetype) initWithItemDictionary:(NSDictionary*)itemDict;

@end

 

AerItems.m

#import "AerItems.h"

@implementation AerItems

@synthesize weatherDate;

@synthesize weatherText;

@synthesize highTemp;

@synthesize lowTemp;

@synthesize weatherDay;

-(id)initWithItemDictionary:(NSDictionary *)itemDict{

    self= [ super init];

        [self setWeatherDate:[itemDict objectForKey:@"date"]];

    [self setWeatherDay:[itemDict objectForKey:@"day"]];

    [self setWeatherText:[itemDict objectForKey:@"text"]];

    [self setHighTemp:[itemDict objectForKey:@"high"]];

    [self setLowTemp:[itemDict objectForKey:@"low"]];

    

    return self;

}

-(NSString*)description{

    

    NSMutableString *descr = [NSMutableString new];

    

    [descr appendString:[NSString stringWithFormat:@"weatherDate: %@", [self weatherDate]]];

    [descr appendString:[NSString stringWithFormat:@"\rweatherText:%@", [self weatherText]]];

    [descr appendString:[NSString stringWithFormat:@"\rhigh:%@",[self highTemp]]];

    [descr appendString:[NSString stringWithFormat:@"\rlow:%@",[self lowTemp]]];

    [descr appendString:[NSString stringWithFormat:@"weatherDay:%@",[self weatherDay]]];

    

    return [NSString stringWithString:descr];

}

-(void)dealloc{

    

    [self setWeatherText:nil];

    [self setWeatherDate:nil];

    [self setWeatherDay:nil];

    [self setHighTemp:nil];

    [self setLowTemp:nil];

    

}

@end

HavaService.h

@interface HavaService : NSObject

+(void)retrieveWeatherForecastForWoeid:(NSString*)woeid withBlock:(void (^)(NSArray* weatherItems))block:

@end

HavaService.m

#import "HavaService.h"

#import "HavaClient.h"

#import "AerItems.h"

@implementation HavaService

static NSString * const kYQLQuery = @"select * from weather.forecast where woeid='%@'";

+(void)retrieveWeatherForecastForWoeid:(NSString *)woeid withBlock:(void (^)(NSArray *weatherItems))block{

    NSMutableArray * weatherItems= [NSMutableArray new];

    

    NSString * query = [NSString stringWithFormat:kYQLQuery, woeid];

    NSDictionary * paramDict= [NSDictionary dictionaryWithObjectsAndKeys:query,@"q" ,@"format" , @"json", nil];

    

    [[HavaClient sharedClient] GET:@"" parameters:paramDict success:^(NSURLSessionDataTask *task, id responseObject) {

        NSDictionary * query = [responseObject objectForKey:@"query"];

        NSDictionary * result = [query objectForKey:@"result"];

        NSDictionary * channel = [result objectForKey:@"channel"];

        NSDictionary *  item = [channel objectForKey:@"item"];

        NSArray * forecast = [item objectForKey:@"forecast"];

        for (NSDictionary * item in forecast){

            AerItems * weatherItem = [[AerItems alloc] initWithItemDictionary:item];

            [weatherItems addObject:weatherItem];

        }

        if (block){

            block ([NSArray arrayWithArray:weatherItems]);

        }

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

        if (block){

            block([NSArray array]);

        }

    }];

}

@end

havaClient.h

#import "AFHTTPSessionManager.h"

@interface HavaClient : AFHTTPSessionManager

+(HavaClient*)sharedClient;

@end

HavaClient.m

 

#import "HavaClient.h"

@implementation HavaClient

 

static NSString * const kYahooBaseURL = @"http://query.yahooapis.com/v1/public/yql";

 

 

 

+(HavaClient*)sharedClient{

    static HavaClient * _sharedClient = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken , ^ {

        NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];

        

        _sharedClient = [[HavaClient alloc] initWithBaseURL:[NSURL URLWithString:kYahooBaseURL] sessionConfiguration:config];

        

        _sharedClient.responseSerializer=[AFJSONResponseSerializer serializer];

       

    });

    

    return _sharedClient;

    

}

@end

AerItemsCell.h

#import <UIKit/UIKit.h>

#import "AerItems.h"

 

 

@interface AerItemsCellTableViewCell : UITableViewCell

 

 

 

 

@property (weak,nonatomic) IBOutlet UILabel *lblDate;

@property (weak,nonatomic) IBOutlet UILabel *lblText;

@property (weak,nonatomic) IBOutlet UILabel *lblDay;

@property (weak,nonatomic) IBOutlet UILabel *lblHighTemp;

@property (weak,nonatomic) IBOutlet UILabel *lblLowTemp;

-(void) setWeatherItem:(AerItems*)WeatherItem;

 

@end

AerItemsCell.m

 

@implementation AerItemsCellTableViewCell

 

@synthesize lblDate;

@synthesize lblDay;

@synthesize lblHighTemp;

@synthesize lblLowTemp;

@synthesize lblText;

 

 

 

 

 

-(void)setWeatherItem:(AerItems *)weatherItem{

    [[self lblDate] setText:[weatherItem weatherDate]];

    [[self lblText] setText:[weatherItem weatherText]];

    [[self lblHighTemp] setText:[weatherItem highTemp]];

    [[self lblLowTemp] setText:[weatherItem lowTemp]];

    [[self lblDay]     setText:[weatherItem weatherDay]];

}

 

@end

 

AerListViewController.h

 

@interface AerListViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>

 

@property (nonatomic,strong) NSString * woeid;

 

@property (weak, nonatomic) IBOutlet UITableView *table;

@property (weak, nonatomic) IBOutlet UILabel *lblTitle;

 

 

 

-(id) initWithWoeid:(NSString*)woeid;

 

@end

AerListViewController.m

#import "AerListViewController.h"

#import "HavaService.h"

#import "AerItemsCellTableViewCell.h"

 

 

@interface AerListViewController ()

 

@property (nonatomic,strong) NSArray * weatherItems;

 

@end

 

@implementation AerListViewController

 

@synthesize table=_table;

@synthesize lblTitle=_lblTitle;

@synthesize woeid=_woeid;

@synthesize weatherItems=_weatherItems;

 

 

 

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}

 

 

 

 

-(id) initWithWoeid:(NSString *)woeid{

    self =[super init];

    [self setWoeid:woeid];

    

    return self;

    

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    [[self lblTitle] setText:[NSString stringWithFormat:@"Aer for %@ " ,[self woeid]]];

    

    [HavaService retrieveWeatherForecastForWoeid:[self woeid] withBlock:^(NSArray *weatherItems){

        [self setWeatherItems:weatherItems];

        [[self table] reloadData];

        

    }];

    

}

 

#pragma  mark - UITableViewDataSource

 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    

    return [_weatherItems count];

 

}

 

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    

    static NSString * CellId= @"AerItemsCell";

    AerItemsCellTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellId];

    if (!cell) {

        NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"AerItemsCellTableViewCell.h" owner:nil options:nil];

        cell = [nib objectAtIndex:0];

        

    }

    

    AerItems * weatherItem =[[self weatherItems] objectAtIndex:[indexPath row]];

    [cell setWeatherItem:weatherItem];

    return cell;

    

}

 

#pragma mark - UITableViewDelegate

 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 64.0;

 

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

[/code]

18 Görüntülenme

3 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Profile picture for user coskungun
coskungun
04.06.2014 - 09:18

Selam Server, 

Göndermiş olduğun kodları inceledim.İlk öncelikle biraz karmaşık yazmışsın. Table için kullanacağın Nsmutablearrayı global fonksiyon içerisinde oluşturmuşsun (instance almışsın)  Arrayı, table için hangi sayfada delegate yada datasource  veriyorsan orda oluşturman lazım. Kodunun çalışmama sebebi ise yahonun apisinin sana text/xml olarak dönmesi.Bu yüzden sonucun failure tarafına düşüyor. Bunun için xml parser kullanman gerek.

Sanırım sen dönen yanıtı json olarak parse etmek istiyorsun ? 

İyi çalışmalar.

server calap
05.06.2014 - 08:34
Merhaba Coskun arrayi dediginiz gibi yapip tekrar deneyecegim ama aslinda donen yaniti json olarak parse etmek istiyorum ama yahoo weather apisinde json tarafli parse olmuyor galiba bi de xml kullanip deneyecegim.Yardimlariniz icin tesekkurler....
picture-6584-1390297533.jpg
servercalap
04.06.2014 - 03:02

Hocam aslinda gelen hata kodu yok simulator calistiginda sadece bos bi tableview goruyorum.

picture-399-1379511904.jpg
halil
04.06.2014 - 02:40

Hocam böyle hiç okunmuyor, kodunu sıkıştırıp dosya ekle şeklinde paylaşırsan çözüm bulman kolaylaşacaktır, ayrıca konsoldan gelen hata notlarını yazman daha faydalı olacaktır, nereye bakmamız gerektiğini biliriz.  

server calap
04.06.2014 - 03:06
Dosyayi yukledim hocam.